package mnc.game.tifo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.StringTokenizer;



public class Util {
	public static String getCurrentDate()
	{
		String s1 = "yyyy-MM-dd";
		
		SimpleDateFormat simpledateformat = new SimpleDateFormat(s1);
		SimpleTimeZone simpletimezone = new SimpleTimeZone(0x1ee6280, "KST");
		simpledateformat.setTimeZone(simpletimezone);
		long l = System.currentTimeMillis();
		Date date = new Date(l);
		
		
		return simpledateformat.format(date);
	}
	/**
	 * 한달전 일자를 얻는다 
	 * @param date
	 * @return
	 */
	public static String getPrevDate(String date) {
		String tmp = "";
		date = getDateNoFormat(date);
		StringBuilder time = new StringBuilder();
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
		Calendar tom2 = new GregorianCalendar();                         //현재시간을 가져와서 객체화
		int year =  Integer.parseInt(date.substring(0, 4));
		int month = Integer.parseInt(date.substring(4, 6));
		int day   = Integer.parseInt(date.substring(6, 8));
//		month = month - 1;
		tom2.set(year, month, day);
		
		tom2.add(Calendar.DATE, -1);       //현재시간에 하루뺌
		
	    Date tomdate2 = tom2.getTime();                           
        tmp = sdf2.format(tomdate2); //yyyy.MM.dd 형식으로 전환
		 
		return tmp;
	}
    public static String getDateNoFormat(String date) {
    	String dt[] = date.split("-");
    	return dt[0] + dt[1] + dt[2];
    }

	/**
	 * 암호화
	 * @param inputStr
	 */
	public static String encodeSTR(String inputStr ) {
		String result = "";
		inputStr = inputStr.trim() + "TIFO_V_1.0";
		
		for(int i = 0; i < inputStr.length(); i++) {
			int val = inputStr.charAt(i) + i * 29;
			result = result + lPad(String.valueOf(val), 0, "3");
			if(i < inputStr.length() -1) result = result + "";
			
		}
		return result;

	}
	
	public static String lPad(String str, int size, String fStr) {
		byte[] b = str.getBytes();
		int len = b.length;
		int tmp = size - len;
		
		for(int i = 0; i < tmp; i++) {
			str = fStr + str;
		}
		
		return str;
	}
	
	/**
	 * 복호화
	 * @param inputStr
	 * @return
	 */
	public static String decodeSTR(String inputStr ) {
		String  result = "";
		inputStr = inputStr.trim() ;
		StringTokenizer st = new StringTokenizer(inputStr,"");
		int i = 0;
		while(st.hasMoreElements()) {
			String tmp = st.nextToken();
			int num = Integer.parseInt(tmp) - (i * 29);
			i++;
			result = result +(char)num;
		}
		result = result.substring(0, result.length()-10);

		return result;
	}
}
